package codingforgreat.class02;

import java.util.HashMap;

public class Class05_SetAll {
    public class MyValue<V>{
        private V value;
        private int times;

        public MyValue(V value, int times) {
            this.value = value;
            this.times = times;
        }
    }
    public class MyHashMap<K,V>{
        private HashMap<K,MyValue<V>> map;
        private int time;
        private MyValue<V> setAll;

        public MyHashMap() {
            map = new HashMap<>();
            time = 0;
            setAll = new MyValue<V>(null, -1);
        }

        public void put(K key, V value) {
           map.put(key,new MyValue<V>(value,time++));
        }

        public void setAll(V value) {
            setAll.value = value;
            setAll.times = time++;
        }

        public V get(K key) {
            if (!map.containsKey(key)) {
                return null;
            }
            if (map.get(key).times > setAll.times) {
                return map.get(key).value;
            } else {
                return setAll.value;
            }
        }

    }
}
